---
title: "conjoint analysis Iraq"
author: "Dai Yamao"
date: "`r format(Sys.time(), '%Y-%m-%d')`"
output:
  html_document: default
  pdf_document: default
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, fig.width = 10, fig.height = 5)
rm(list=ls())

require(tidyverse)
require(cjoint)
library(cregg)
library(scales)
library(patchwork)
library(extrafont)
require(memisc)
require(stringi)
require(ggpubr)

# PL
dat_pl <- read_csv("data/cj_data_Palestine.csv")
dat_pl <- na.exclude(dat_pl)

dat_pl <- dat_pl |>
  dplyr::select(RecordedDate, Q2_2, task, profile, 
                selected, respondentIndex, 
                Activity, Armament, Patron, Service)
dat_pl$Country <- "Palestine"
dat_pl <- dat_pl |>
  mutate(Patron = dplyr::recode(Patron,
                         "Palestinian government" = "Own government",
                         "Iran" = "Iran"))
str(dat_pl)
dat_pl[,c(7:10)] <- data.frame(lapply(dat_pl[,c(7:10)],as.factor))

# LB
dat_lb <- read_csv("data/cj_data_Lebanon.csv")
dat_lb <- na.exclude(dat_lb)

dat_lb <- dat_lb |>
  dplyr::select(RecordedDate, Q2_2, task, profile, 
                selected, respondentIndex, 
                Activity, Armament, Patron, Service)
dat_lb$Country <- "Lebanon"
dat_lb <- dat_lb |>
  mutate(Patron = dplyr::recode(Patron,
                         "Lebanese government" = "Own government",
                         "Iran" = "Iran"))
str(dat_lb)
dat_lb[,c(7:10)] <- data.frame(lapply(dat_lb[,c(7:10)],as.factor))

# IQ
dat_iq <- read_csv("data/cj_data_Iraq.csv")
dat_iq <- na.exclude(dat_iq)

dat_iq <- dat_iq |>
  dplyr::select(RecordedDate, Q2_2, task, profile, 
                selected, respondentIndex, 
                Activity, Armament, Patron, Service)
dat_iq$Country <- "Iraq"
dat_iq <- dat_iq |>
  mutate(Patron = dplyr::recode(Patron,
                         "Iraqi government" = "Own government",
                         "Iran" = "Iran"))
str(dat_iq)
dat_iq[,c(7:10)] <- data.frame(lapply(dat_iq[,c(7:10)],as.factor))

# YM
dat_ym <- read_csv("data/cj_data_Yemen.csv")
dat_ym <- na.exclude(dat_ym)

dat_ym <- dat_ym |>
  dplyr::select(RecordedDate, Q2_2, task, profile, 
                selected, respondentIndex, 
                Activity, Armament, Patron, Service)
dat_ym$Country <- "Yemen"
dat_ym <- dat_ym |>
  mutate(Patron = dplyr::recode(Patron,
                         "Yemeni government" = "Own government",
                         "Iran" = "Iran"))
str(dat_ym)
dat_ym[,c(7:10)] <- data.frame(lapply(dat_ym[,c(7:10)],as.factor))

# merge
dat1 <- dplyr::full_join(dat_pl, dat_lb)
dat2 <- dplyr::full_join(dat_iq, dat_ym)
dat <- dplyr::full_join(dat1, dat2)

dat$Country <- as.factor(dat$Country) 
```


# whole model
```{r, fig.height = 7, dpi = 300}
# amce
country_amce <- cj(data = dat,
                 selected ~ Activity + Armament + Patron + Service,
                 id = ~ respondentIndex,
                 estimate = "amce",
                 by = ~ Country,
                 alpha = 0.1)
country_amce_diff <- cj(data = dat,
                      selected ~ Activity + Armament + Patron + Service,
                      id = ~ respondentIndex,
                      estimate = "amce_diff",
                      by = ~ Country,
                      alpha = 0.1)
plot(rbind(country_amce, country_amce_diff), size = 2) + 
  ggplot2::facet_wrap(~ BY, ncol = 3L)
plot(country_amce, group = "Country", size = 2)


# MM
country_mm <- cj(data = dat,
                 selected ~ Activity + Armament + Patron + Service,
                 id = ~ respondentIndex,
                 estimate = "mm",
                 by = ~ Country,
                 alpha = 0.1)
country_mm_diff <- cj(data = dat,
                      selected ~ Activity + Armament + Patron + Service,
                      id = ~ respondentIndex,
                      estimate = "mm_diff",
                      by = ~ Country,
                      alpha = 0.1)
plot(country_mm, group = "Country", size = 2)

# Plot
whole_result <- plot(country_amce, group = "Country", size = 2)

ggsave(filename = "result/whole_result.png",
       plot = whole_result,
       width = 7,
       height = 6,
       units = "in", 
       dpi = 600)

```

